using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Browser.Exceptions;
using RJB.S3.Entities;
using RJB.S3Library;
using Browser.Helpers;

namespace Browser
{
    public partial class frmAddFile : Form
    {

        private string _folderName;
        
        public string FolderName
        {
            get { return _folderName; }
            set { _folderName = value; }
        }
        
        
        public frmAddFile()
        {
            InitializeComponent();
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtChoseFile_Click(object sender, EventArgs e)
        {
            ofdChoseFhile.ShowDialog(this);
            if ( ofdChoseFhile.FileName.Length > 0 )
            {
                txtFile.Text = ofdChoseFhile.FileName;
            }
        }

        private void butUpload_Click(object sender, EventArgs e)
        {
            ValidateForm();
            FileManager _fm = new FileManager(Utils.RetrieveUserKey(), Utils.RetrieveSecretKey());
            File _file = new File(txtFileName.Text, this.FolderName, txtOwner.Text);
            _file.Contents = Utils.ReadFile(txtFile.Text);

            if (dgvMetaData.Rows.Count > 1)
            {
                List<MetadataEntry> _metaDataRecords = new List<MetadataEntry>(dgvMetaData.Rows.Count - 1);
                foreach (DataGridViewRow row in dgvMetaData.Rows)
                {
                    MetadataEntry _metaData = new MetadataEntry();
                    _metaData.Name = row.Cells[0].Value.ToString();
                    _metaData.Value = row.Cells[1].Value.ToString();
                }
                _file.MetaData = _metaDataRecords;
            }
            
            _fm.SaveFile(_file);
            
            ((frmMain) this.Owner).PopulateFiles(this.FolderName);
            this.Close();
        }
        
        private void ValidateForm()
        {
            StringBuilder _message = new StringBuilder();
            
            if ( txtFile.Text.Length <= 0 )
            {
                _message.Append("Please chose a valid file.");
            }
            if ( txtFileName.Text.Length <= 0 )
            {
                _message.Append("Please chose a valid file name.");
            }
            if ( txtOwner.Text.Length <= 0 )
            {
                _message.Append("Please enter an owner for the file.");
            }

            //Check for duplicate keys
            if ( dgvMetaData.Rows.Count > 1 )
            {
                List<string> _keys = new List<string>(dgvMetaData.Rows.Count - 1);
                foreach (DataGridViewRow row in dgvMetaData.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        if (_keys.Contains(row.Cells[0].Value.ToString()))
                        {
                            _message.Append("There are duplicate keys in the metadata.");
                        }
                        else
                        {
                            _keys.Add(row.Cells[0].Value.ToString());
                        }
                    }
                }
            }
            
            if ( _message.Length > 0 )
            {
                throw new ValidationException(_message.ToString());
            }
        }
    }
}